import json
from typing import Dict, Any

from fastapi import HTTPException, Depends

from .base_router import BaseRouter
from ..config import settings
from ..designer.strategy.chunk.jieba_chunking_strategy import JiebaChunkingStrategy
from ..schemas.civil_exam_schemas import PositionRecommendationRequest

from ..schemas.common import ApiResponse
from ..services.chunk_service import ChunkService
from app.excepetions.rag_exception import RAGException
import logging

from ..services.civil_exam_service import CivilExamRAGService

logger = logging.getLogger("ChunkRouter")

# 配置
KB_BUCKET_NAME = settings.KB_BUCKET_NAME

# 支持的文件类型
class ChunkRouter(BaseRouter):
    def __init__(self):
        logger.info("Initializing ChunkRouter")
        super().__init__()
        self.router = self._register_routes()

    def _register_routes(self):
        """注册所有路由处理方法并添加Swagger文档注解"""

        # 解析文件
        self.router.post(
            "/parseFile",
            response_model=ApiResponse,
            summary="解析知识文件",
            description="对已上传的文件进行解析，提取文本内容并分割为可索引的片段",
            tags=["知识管理"]
        )()
        return self.router
    #获取公考选岗服务实例
    def get_civil_exam_service(self) -> CivilExamRAGService:
        """获取公考选岗服务实例"""
        return CivilExamRAGService()
    #根据用户信息推荐合适的公务员岗位
    async def recommend_positions(
            request: PositionRecommendationRequest,
            service: CivilExamRAGService = Depends(get_civil_exam_service)
    ):
        """
        根据用户信息推荐合适的公务员岗位

        Args:
            request: 岗位推荐请求，包含用户信息
            service: 公考选岗服务实例

        Returns:
            推荐的岗位列表和相关信息
        """
        try:
            logger.info(f"开始为用户推荐岗位，用户ID: {request.user_info.user_id}")

            result = service.recommend_positions(request.user_info)

            if result["code"] != 200:
                raise HTTPException(status_code=500, detail=result["message"])

            return result
        except Exception as e:
            logger.error(f"岗位推荐接口异常：{str(e)}")
            raise HTTPException(status_code=500, detail=f"服务异常：{str(e)}")





